import {Context, Next} from 'koa';
import {getErrorMsg} from '@/utils/error';
import {WrapperResponse} from '@/utils/wrapperResponse';

const catchGlobalError = () => async (ctx: Context, next: Next) => {
    return next().catch(err =>  {
        const errMsg = getErrorMsg(err);
        const body = WrapperResponse.error(errMsg);
        Object.assign(ctx, {body, status: body.status});
    });
};

export default catchGlobalError;
